// pages/comContext/comContext.js
var _id=""
var replyItem={}
// let starItem={}
var nickName=""
var url=""
// let openid=""
var dateTime=""
var zanChange=""
var starChange=""
var util=require('../../utils/util')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    arrayIndex:0,
    zan:0,
    star:0,
    type:false,
    flag:true,
    bool:true,
    showDelete:false,
    zanNumber:0,
    num:-1,
    index:0,
    indexnumber:0,
    openid:"",
    content:"",
    context:"",
    lander:"",
    landContext:"",
    starItem:{},
    topicList:[],
    land:[],
    starArray:[],
    imageList:[]
  },
  preview(event) {
    console.log(event.currentTarget.dataset.src)
    var currentUrl = event.currentTarget.dataset.src
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: this.data.imageList // 需要预览的图片http链接列表
    })
  },
  delete(e){
    var that=this
    console.log(e.currentTarget.dataset.indexnumber)
    that.setData({
      showDelete:true,
      indexnumber:e.currentTarget.dataset.indexnumber
    })
  },
  
  cancel(){
    var that=this
    that.setData({
      showDelete:false
    })
  },
  deleteButton()
  {
    console.log("删除")
    wx.cloud.callFunction({
      name:"updata",
      data:{
        id:_id,
        action:"landList_delete",
        index:this.data.indexnumber
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    // console.log(options.star)
    // let topicList=JSON.parse(options.topicList)
    // console.log(topicList)
    nickName=wx.getStorageSync('nickName')
    url=wx.getStorageSync('url')
    if(options.star!=undefined)
    {
      console.log(1)
      that.setData({
        openid:wx.getStorageSync('openid'),
        arrayIndex:options.index,
        star:options.star
      })
    }
    else
    {
      console.log(2)
      that.setData({
        openid:wx.getStorageSync('openid'),
        // arrayIndex:options.index,
        // star:options.star
        // star:options.star
      })
      
    }
    that.setData({
      openid:wx.getStorageSync('openid'),
      arrayIndex:options.index,
      star:options.star
      // star:options.star
    })
    if (!wx.getStorageSync('nickName')||!wx.getStorageSync('url')||!wx.getStorageSync('openid')) 
    {
     wx.showToast({
       title: '请先登录后再使用社区功能',
     }) 
    }
    _id=options.id
    console.log(_id)
  },
  onShow(){
    wx.showLoading({
      title: '加载中',
    })
    var that=this
    replyItem={}
    starChange=that.data.star
    wx.cloud.database().collection("recodeList").where({
      _id:_id
    }).get({
      success(res){
        console.log(res)
        zanChange=res.data[0].zan
        that.setData({
          topicList:res.data,
          land:res.data[0].land,
          zanNumber:res.data[0].zan,
          imageList:res.data[0].image
        })
      }
    })
    wx.hideLoading({
    })
  },
  onHide(){
    console.log("zanChange",zanChange)
    console.log("zanNumber",this.data.zanNumber)
    console.log("starChange",starChange)
    console.log("star",this.data.star)
    if (zanChange!=this.data.zanNumber) {
      console.log("进入onHide")
      wx.cloud.callFunction({
        name:"updata",
        data:{
          id:_id,
          action:"topicList_zan",
          zan:this.data.zanNumber
        }
      })
    }
    if (starChange!=this.data.star) {
      console.log("进入onHide")
      wx.cloud.database().collection("userLisrt").where({
        openid:openid
      }).get({
        success(res){
          console.log(res)
          star=res.data[0].star
        }
      })
      starItem.id=_id
      starItem.title=this.data.topicList[0].title
      star=star.push(starItem)
      wx.cloud.callFunction({
        name:"update",
        data:{
          action:"userList_star",
          openid:this.data.openid,
          star:star
        }
      })
    }
  },
  onUnload(){
    var that=this
    var arrayIndex=that.data.arrayIndex
    var starItem=that.data.starItem
    // console.log("zanChange",zanChange)
    // console.log("zanNumber",this.data.zanNumber)
    // console.log("starChange",starChange)
    // console.log("star",this.data.star)
    if (zanChange!=this.data.zanNumber) {
      console.log("进入onunload")
      wx.cloud.callFunction({
        name:"updata",
        data:{
          id:_id,
          action:"topicList_zan",
          zan:this.data.zanNumber
        }
      })
    }
    if (starChange!=this.data.star) {
      if(this.data.star==1)
      {
        starItem.id=_id
        starItem.title=this.data.topicList[0].title
        starItem.text=this.data.topicList[0].context
        var starItem = Object.assign({},that.data.starItem);
        wx.cloud.callFunction({
          name:"updata",
          data:{
            action:"userList_star",
            openid:that.data.openid,
            star:starItem
          }
        })
      }
      else
      {
        wx.cloud.database().collection("userList").where({openid:this.data.openid}).get({
          success(res){
            that.setData({
              starArray:res.data[0].star.splice(arrayIndex,1)
            })
            wx.cloud.callFunction({
              name:"updata",
              data:{
                action:"userList_star_delete",
                openid:that.data.openid,
                star:starArray
              }
            })
          }
        })
      }
    }
  },
  tapZan()
  {
    var zan=this.data.zan
    var zanNumber=this.data.zanNumber
    if(zan==1)
    {
      this.setData({
        zan:0,
        zanNumber:zanNumber-1
      })
      wx.removeStorageSync('zanNumber')
    }
    else if(zan==0)
    {
      this.setData({
        zan:1,
        zanNumber:zanNumber+1
      })
      wx.setStorageSync('zanNumber', zanNumber)
    }
  },
  tapStar()
  {
    console.log('点击了收藏按钮')
    // var that=this
    var star=this.data.star
    if(star==1)
    {
      this.setData({
        star:0, 
      })
      wx.removeStorageSync('star')
      wx.showToast({
        icon:"none",
        title: '取消收藏',
      })
    }
    else if(star==0)
    {
      this.setData({
        star:1,
      })
      wx.setStorageSync('star', star)
      wx.showToast({
        title: '收藏成功',
      })
    }
  },
  replyLander(e)
  {
    var that=this
    lander=e.target.dataset.replyuser
    var content=e.target.dataset.content
    var type=that.data.type
     that.setData({
       content:content,
       type:~type
     })
  },

  changeto1()
  {
    var that=this
    that.setData({
      flag:false
    })
  },
  changeto2()
  {
    var that=this
    that.setData({
      flag:true
    })
  },
  boolChange()
  {
    var that=this
    var bool=that.data.bool
    that.setData
    ({
      bool:!bool
    })
  },
  compare: function(property) {
    return function(a, b) {
      var value1 = a[property];
      var value2 = b[property];
      return value2 - value1;
    }
  },
  getInput(e){
    var that=this
    that.setData({
      context:e.detail.value
    })
  },
  addInput()
  {
    var that=this
    if(that.data.context.length==0)
    {
      wx.showToast({
        title: '请输入要回复的内容',
        icon:"none"
      })
    }
    else
    {
      var that=this
      var index=that.data.index
      var openid=that.data.openid
      dateTime=util.formatTime2(new Date())
      // let reply=[]
      replyItem.name=nickName
      replyItem.context=that.data.context
      replyItem.image=url
      replyItem.openid=openid
      replyItem.dateTime=dateTime
      // replyItem.reply=reply
      that.data.topicList[index].land.push(replyItem)
      var land=that.data.topicList[index].land
      wx.showLoading({
        title: '发表中',
      })
      wx.cloud.callFunction({
        name:"updata",
        data:{
          id:_id,
          action:"topicList_update",
          land:land
        }
      })
      .then(res=>{
        that.setData({
          land:land,
          context:""
        })
        wx.hideLoading({
        })
      })
    }
    that.onShow()
  },
  // addlanderInput(){
  //   let that=this
  //   if(that.data.context.length==0)
  //   {
  //     wx.showToast({
  //       title: '请输入要回复的内容',
  //       icon:"none"
  //     })
  //   }
  //   else
  //   {
  //     let that=this
  //     let index=that.data.index
  //     replyItem.context=that.data.context
  //     replyItem.lander=lander
  //     replyItem.replyUser=wx.getStorageSync('nickName')
  //     console.log(replyItem)
  //     that.data.topicList[index].land[0].reply.push(replyItem)
  //     let land=that.data.topicList[index].land[0].reply
  //     wx.showLoading({
  //       title: '发表中',
  //     })
  //     wx.cloud.callFunction({
  //       name:"updata",
  //       data:{
  //         id:_id,
  //         action:"lander_update",
  //         'land.0':land
  //       }
  //     })
  //     .then(res=>{
  //       that.setData({
  //         reply:land,
  //         context:""
  //       })
  //       wx.hideLoading({
  //       })
  //     })
  //   }
  // },
  changeReply:function(e) {
  {
    console.log(e.currentTarget.dataset.num)
    this.setData({
      bool:true,
      num:e.currentTarget.dataset.num
    })
  }
}
})